<?php
/**
 * Created by PhpStorm.
 * User: qttx
 * Date: 2019/5/31
 * Time: 15:53
 */

namespace app\api\model;
use think\Db;
class UserModel extends BaseModel
{
    protected $table = "xst_user";
    protected $rule = [
        'id' => '',
    ];
    protected $msg = [
        'id' => '',
    ];

    /**
     * 注册
     */
    public function registerd()
    {
        $XST         = new XstModel();
      	//会员收款地址
        $r           = $XST->getAddress();
        $xst_address = $r['address'];
        $xst_key     = $r['privateKey'];
      	//商家收款地址
      	$shop = $XST->getAddress();
      	$shop_address = $shop['address'];
        $shop_key     = $shop['privateKey'];
      	
        $nickname    = request()->post('nickname');
        $login_pwd   = request()->post('login_pwd');
        $login_pwds  = request()->post('login_pwds');
        $phone       = request()->post('phone');
        $smcode      = request()->post('smcode');
        $code        = request()->post('code');
        $codes       = rand(100000, 999999);
      	$sql = $this->where('phone',$phone)->select();
      	if(!empty($sql))
        {
        	return $stu = ['code' => 0, 'message' => '该手机号已被注册'];
        }
        $is_code     = $this->where("code", $codes)->find();
        while ($is_code) {
            $codes   = rand(100000, 999999);
            $is_code = $this->where("code", $codes)->find();
        }
        if ($login_pwd != $login_pwds) {
            return $stu = ['code' => 0, 'message' => '两次密码不一致'];
        }
        $sm  = new SmModel();
        $res = $sm->where('phone', $phone)
            ->order('id desc')
            ->limit(0, 1)
            ->select();
        if($res[0]['code']!=$smcode&&$res[0]['invalidtime']<=time())
        {
            return $stu = ['code' => 0, 'message' => '验证码不正确'];
        }
        if (!empty($code)) {
            $recommend = $this->where('code', $code)->find();
            $pid       = $recommend['id'];
            $data      = [
                'nickname'    => $nickname,
                'phone'       => $phone,
                'login_pwd'   => authcode($login_pwd, 'ENCODE'),
                'pid'         => $pid,
                'path'        => $recommend['path'] . $pid . ',',
                'code'        => $codes,
                'type'        => 1,
                'status'      => 1,
                'star'        => 0,
                'createtime'  => time(),
                'token'       => md5($codes),
                'xst_address' => $xst_address,
                'xst_key'     => $xst_key,
              	'shop_address' => $shop_address,
                'shop_key'     => $shop_key
            ];
        } else {
            $data = [
                'nickname'    => $nickname,
                'phone'       => $phone,
                'login_pwd'   => authcode($login_pwd, 'ENCODE'),
                'pid'         => 0,
                'code'        => $codes,
                'type'        => 1,
                'status'      => 1,
                'star'        => 0,
                'createtime'  => time(),
                'token'       => md5($codes),
                'xst_address' => $xst_address,
                'xst_key'     => $xst_key,
              	'shop_address' => $shop_address,
                'shop_key'     => $shop_key
            ];
        }
        $result = $this->insert($data);
        $addBblance=array(
          "uid"=>$this->getLastInsID(),
        );
        $res=Db::name("balance")->insert($addBblance);
      	if($result)
        {
        	return $stu = ['code' => 1, 'message' => '注册成功'];
        }else{
        	return $stu = ['code' => 0, 'message' => '注册失败'];
        }
    }

    /**
     * 登陆
     */
    public function login()
    {
        $phone  = request()->post('phone');
        $login_pwd = request()->post('login_pwd');
        $res = $this->where('phone', $phone)->find();
        if (authcode($res['login_pwd'], 'DECODE') == $login_pwd) {
          	return array('code' => 1, 'message' => '登陆成功',"token"=>$res['token']);
        } else {
            return $stu = ['code' => 10, 'message' => '账号或密码不正确'];
        }
    }
}